<?php
$signature = $_GET['signature'];//获取get传递的参数
$timestamp = $_GET['timestamp'];
$nonce     = $_GET['nonce'];
$echostr   = $_GET['echostr'];
$token     ='ilife';//用户自己设置的token
$tmpArr    =array($token,$timestamp,$nonce);//构造数组将字符串放到里边
sort($tmpArr, SORT_STRING);//数组元素升降排序
$tmpStr=implode('', $tmpArr);//数组元素分隔为字符串
$return_str=sha1($tmpStr);//字符串sha1加密
if ($return_str==$signature && $echostr) { //是否是第一次接入微信api
    echo $echostr;
} else {
    responseMsg();
}
 function responseMsg()
    {
        //get post data, May be due to the different environments
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

        //extract post data
        if (!empty($postStr)){
                /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,
                   the best way is to check the validity of xml by yourself */
                libxml_disable_entity_loader(true);
                $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
                $fromUsername = $postObj->FromUserName;
                $toUsername = $postObj->ToUserName;
                $keyword = trim($postObj->Content);
                $time = time();

                //输入关键字回复内容
                if ($postObj->MsgType == 'text') {
                    if(!empty( $keyword ))
                    {
                        $textTpl = "<xml>
                                    <ToUserName><![CDATA[%s]]></ToUserName>
                                    <FromUserName><![CDATA[%s]]></FromUserName>
                                    <CreateTime>%s</CreateTime>
                                    <MsgType><![CDATA[%s]]></MsgType>
                                    <Content><![CDATA[%s]]></Content>
                                    <FuncFlag>0</FuncFlag>
                                    </xml>";
                        $msgType = "text";
                        $contentStr = "Welcome to wechat world!";
                        $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                        echo $resultStr;
                    }else{
                        echo "Input something...";
                    }
                }

                //事件回复消息
                if ($postObj->MsgType == 'event') {
                    //关注
                    if ($postObj->Event == 'subscribe') {
                        $msgType = "text";
                        //扫描二维码关注
                        if (isset($postObj->EventKey)) {
                            $textTpl = "<xml>
                                        <ToUserName><![CDATA[%s]]></ToUserName>
                                        <FromUserName><![CDATA[%s]]></FromUserName>
                                        <CreateTime>%s</CreateTime>
                                        <MsgType><![CDATA[%s]]></MsgType>
                                        <Content><![CDATA[%s]]></Content>
                                        </xml>";
                            $contentStr = "https://v.qq.com";
                        } else {
                            $textTpl = "<xml>
                                        <ToUserName><![CDATA[%s]]></ToUserName>
                                        <FromUserName><![CDATA[%s]]></FromUserName>
                                        <CreateTime>%s</CreateTime>
                                        <MsgType><![CDATA[%s]]></MsgType>
                                        <Content><![CDATA[%s]]></Content>
                                        </xml>";
                           $contentStr = "欢迎关注本活动测试号!";
                        }

                        $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                        echo $resultStr;
                    }

                    if (strtolower($postObj->Event) == 'click') {

                        if ($postObj->EventKey == 'V1001_GOOD') {

                            $textTpl = "<xml>
                                        <ToUserName><![CDATA[%s]]></ToUserName>
                                        <FromUserName><![CDATA[%s]]></FromUserName>
                                        <CreateTime>%s</CreateTime>
                                        <MsgType><![CDATA[%s]]></MsgType>
                                        <Content><![CDATA[%s]]></Content>
                                        </xml>";
                           $contentStr = "感谢您的点赞!";
                           $msgType = "text";
                           $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                            echo $resultStr;

                        }

                        if ($postObj->EventKey =='V1001_TODAY_MUSIC') {
                            $textTpl = "<xml>
                                        <ToUserName><![CDATA[%s]]></ToUserName>
                                        <FromUserName><![CDATA[%s]]></FromUserName>
                                        <CreateTime>%s</CreateTime>
                                        <MsgType><![CDATA[%s]]></MsgType>
                                        <ArticleCount>1</ArticleCount>
                                        <Articles>
                                        <item>
                                        <Title><![CDATA[%s]]></Title>
                                        <Description><![CDATA[%s]]></Description>
                                        <PicUrl><![CDATA[%s]]></PicUrl>
                                        <Url><![CDATA[%s]]></Url>
                                        </item>
                                        </Articles>
                                        </xml> ";
                           $msgType ='news';
                           $title="老兵照顾瘫痪妻子 21年相濡以沫";
                           $description="2016年11月13日，甘肃平凉崆峒区保丰安置区高奉勤家，当天正值高奉勤老伴尚月珍68岁生日，记者采访时，年过七旬的退伍老兵高奉勤正忙着照顾瘫痪在床长达21年的妻子尚月珍。";
                            $picurl="http://img1.gtimg.com/18/1814/181454/18145495_980x1200_0.jpg";
                            $arturl="http://news.qq.com/a/20161114/000407.htm#p=1";
                           $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $title,$description,$picurl,$arturl);
                            echo $resultStr;
                        }

                    }
                }


        }else {
            echo "";
            exit;
        }
    }
